const {
	addFood,
	getFoodInfoById,
	modifyFood,
	getAllFoodInfo,
	deleteFood,
	getFoodInfo
} = require('../service/food.service')

class FoodsController {
	async addFoods(ctx) {
		const { food_name, price, tag, address, detail, comments, mark } = ctx.request.body
		const res = await addFood(food_name, price, tag, address, detail, comments, mark)
		ctx.body = {
			code: 0,
			message: '添加商品成功',
			result: {
				food_name: res.food_name,
				price: res.price,
				tag: res.tag,
				address: res.address,
				detail: res.detail,
				comments: res.comments,
				mark: res.mark
			}
		}
	}
	async modifyFoods(ctx) {
		const id = ctx.request.query.id
		const { food_name, price, tag, address } = ctx.request.body
		try {
			const res = await getFoodInfoById(id)
			if (res) {
				// 更新数据
				await modifyFood({ food_name, price, tag, address }, id)
				ctx.body = {
					code: 0,
					message: '修改信息成功'
				}
			}
			else {
				return ctx.app.emit('error', invalidId, ctx)
			}
		}
		catch (error) {
			console.error("出错");
		}

	}
	async getAllFoods(ctx) {
		try {
			const res = await getAllFoodInfo()
			ctx.body = {
				res
			}
		} catch (err) {
			console.error('出错');
		}
	}
	async deleteFoods(ctx) {
		const id = ctx.request.query.id
		console.log(id);
		try {
			const res = await getFoodInfoById(id)
			if (res) {
				try {
					await deleteFood(id)
					ctx.body = {
						code:0,
						message:'删除成功'
					}
				} catch (err) {
					console.error("出错");
				}
				
			}
			else {
				console.error("出错");
			}
		} catch (err) {
			console.error("出错");
		}
	}
	async getFoods(ctx){
		const { food_name, price, tag, address } = ctx.request.body
		const res = await getFoodInfo({ food_name, price, tag, address })
		ctx.body = {
			res
		}
	}
	async getFood(ctx){
		const {pageNum,pageSize} = ctx.request.query
		console.log(pageNum,pageSize);
	}
}
module.exports = new FoodsController()